﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AddIn.EA.Architecture.Presentation
{
    public partial class CreateStandardProjectImportEntityAttributes : BaseAddInArchitecture
    {
        #region Local Objects
        private ComponentLogic.StandardProject.BusinessEntityStandardProject SelectedEntity;
        private List<ComponentLogic.StandardProject.BusinessEntityStandardProject> Entities;
        #endregion

        #region Start Form
        public CreateStandardProjectImportEntityAttributes(List<ComponentLogic.StandardProject.BusinessEntityStandardProject> entities)
        {
            InitializeComponent();
            base.LoadAddInControls += new EventHandler(CreateStandardProjectImportEntityAttributes_LoadAddInControls);
            this.Entities = entities;
        }
        void CreateStandardProjectImportEntityAttributes_LoadAddInControls(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
        private void CreateStandardProjectImportEntityAttributes_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                LoadEntities();
            }
            catch (Exception ex)
            {
                HandleException("Inicializar a tela de importação de atributos.", ex);
            }
        }
        #endregion

        #region Events
        private void lstEntities_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadEntityAttributes();
        }
        private void cmdConfirm_Click(object sender, EventArgs e)
        {
            ConfirmSelection();
        }
        #endregion

        #region Methods
        private void LoadEntities()
        {
            try
            {
                lstEntities.Items.Clear();
                if (Entities != null && Entities.Count > 0)
                {
                    foreach (ComponentLogic.StandardProject.BusinessEntityStandardProject itemEntity in Entities)
                    {
                        ListViewItem listItem = new ListViewItem(itemEntity.Name);
                        listItem.SubItems.Add(itemEntity.NamePlural);
                        lstEntities.Items.Add(listItem);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Carregar a lista de Entidades", ex);
            }
        }
        private void LoadEntityAttributes()
        {
            try
            {
                lstAttributes.Items.Clear();

                if (lstEntities.SelectedItems.Count == 1)
                {
                    SelectedEntity = Entities.Find(it => it.Name.CompareTo(lstEntities.SelectedItems[0].SubItems[0].Text) == 0);

                    if (SelectedEntity != null &&
                        SelectedEntity.Properties != null &&
                        SelectedEntity.Properties.Count > 0)
                    {
                        foreach (ComponentLogic.StandardProject.PropertyStandardProject item in SelectedEntity.Properties)
                        {
                            ListViewItem listItem = new ListViewItem(item.Name);
                            listItem.SubItems.Add(item.Type);
                            listItem.SubItems.Add(item.Scope.ToString());
                            listItem.SubItems.Add(item.Stereotype);
                            listItem.SubItems.Add(GetBooleanYesNoDescription(item.ComposeFilter));
                            listItem.SubItems.Add(GetBooleanYesNoDescription(item.ComposeKey));
                            listItem.SubItems.Add(GetBooleanYesNoDescription(item.Encapsulate));
                            listItem.SubItems.Add(GetBooleanYesNoDescription(item.IsStatic));
                            listItem.SubItems.Add(GetBooleanYesNoDescription(item.IsConstant));
                            lstAttributes.Items.Add(listItem);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Carregar a lista de atributos/propriedades da classe", ex);
            }
        }
        private void ConfirmSelection()
        {
            try
            {
                if (lstAttributes.CheckedItems.Count > 0)
                {
                    if (MessageBox.Show("Confirma a importação dos atributos selecionados para a classe em edição?", "Questão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                        List<ComponentLogic.StandardProject.PropertyStandardProject> attributes = new List<AddIn.EA.Architecture.ComponentLogic.StandardProject.PropertyStandardProject>();
                        foreach (ListViewItem listItem in lstAttributes.CheckedItems)
                        {
                            ComponentLogic.StandardProject.PropertyStandardProject itemProperty = SelectedEntity.Properties.Find(it => it.Name.CompareTo(listItem.SubItems[0].Text) == 0) as ComponentLogic.StandardProject.PropertyStandardProject;
                            if (itemProperty != null)
                                attributes.Add(itemProperty);
                        }

                        this.Tag = attributes;
                        this.Close();
                    }
                }
                else
                    MessageBox.Show("Selecione ao menos um atributo para importar!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                HandleException("Confirmar a seleção dos atributos para importação.", ex);
            }
        }
        #endregion
    }
}
